import { createStore } from 'vuex'

export default createStore({
    state: {
        // 用户信息
        userInfo: {},
        // 详情页标题
        detailTitle: '',
        // 评论列表
        commentList: [],
        // 收藏列表
        favouriteMovies: {}
    },
    getters: {
        getFavouriteMovieById: (state) => (id) => {
            return state.favouriteMovies[id]
        }
    },
    mutations: {
        setUser(state, obj){
            state.userInfo = obj
        },
        setTitle(state, str) {
            state.detailTitle = str
            // 修改页面标题
            document.title = str
        },
        setCommentList(state, obj) {
            state.commentList.push(obj)
        },
        // 修改或设置收藏
        setFavouriteMovie(state, obj) {
            state.favouriteMovies[obj.id] = obj
        },
        // 删除收藏
        deleteFavouriteMovie(state, id) {
            delete state.favouriteMovies[id]
        }
    }
})